import json
from flask import request, jsonify
from src import common
from utils.initApp import default_response

from . import auth
from .service import AuthService


@auth.route("/login", methods=['POST'])
def login():
    data = json.loads(request.get_data(as_text=True))

    values = dict()
    values['userName'] = data["userName"]
    values['password'] = data["password"]

    result, code, msg = authService.login(values)
    response = default_response.construct_response(result, code, msg)
    return response


@auth.route("/register", methods=['POST'])
def register():
    data = json.loads(request.get_data(as_text=True))

    values = dict()
    values["userName"] = data["userName"]
    values['password'] = data["password"]

    result = authService.register(values)
    response = default_response.construct_response(result)
    return response


@auth.route("/forget", methods=['POST'])
def forget():
    data = json.loads(request.get_data(as_text=True))

    values = dict()
    values["email"] = data["email"]

    result, msg, code = authService.forget(values)
    response = default_response.construct_response(result, msg, code)
    return response


@auth.route("/reset/<reset_token>", methods=['GET'])
def reset(reset_token):

    values = dict()
    values["token"] = reset_token

    result = authService.reset(values)
    response = result
    return response


authService = AuthService(['username', 'id'])
